{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Water Filling in Communications\n",
    "by Robert Gowers, Roger Hill, Sami Al-Izzi, Timothy Pollington and Keith Briggs\n",
    "\n",
    "from Boyd and Vandenberghe, Convex Optimization, example 5.2 page 145\n",
    "\n",
    "Convex optimisation can be used to solve the classic water filling problem.  This problem is where a total amount of power $P$ has to be assigned to $n$ communication channels, with the objective of maximising the total communication rate.  The communication rate of the $i$th channel is given by:\n",
    "\n",
    "$\\log(\\alpha_i + x_i)$\n",
    "\n",
    "where $x_i$ represents the power allocated to channel $i$ and $\\alpha_i$ represents the floor above the baseline at which power can be added to the channel.  Since $-\\log(X)$ is convex, we can write the water-filling problem as a convex optimisation problem:\n",
    "\n",
    "minimise $\\sum_{i=1}^N -\\log(\\alpha_i + x_i)$ \n",
    "\n",
    "subject to $x_i \\succeq 0$ and $\\sum_{i=1}^N x_i = P$\n",
    "\n",
    "This form is also very straightforward to put into DCP format and thus can be simply solved using CVXPY."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!/usr/bin/env python3\n",
    "# @author: R. Gowers, S. Al-Izzi, T. Pollington, R. Hill & K. Briggs\n",
    "import numpy as np\n",
    "import cvxpy as cp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def water_filling(n, a, sum_x=1):\n",
    "    '''\n",
    "    Boyd and Vandenberghe, Convex Optimization, example 5.2 page 145\n",
    "    Water-filling.\n",
    "  \n",
    "    This problem arises in information theory, in allocating power to a set of\n",
    "    n communication channels in order to maximise the total channel capacity.\n",
    "    The variable x_i represents the transmitter power allocated to the ith channel, \n",
    "    and log(α_i+x_i) gives the capacity or maximum communication rate of the channel. \n",
    "    The objective is to minimise -∑log(α_i+x_i) subject to the constraint ∑x_i = 1 \n",
    "    '''\n",
    "    \n",
    "    # Declare variables and parameters\n",
    "    x = cp.Variable(shape=n)\n",
    "    alpha = cp.Parameter(n, nonneg=True)\n",
    "    alpha.value = a\n",
    "\n",
    "    # Choose objective function. Interpret as maximising the total communication rate of all the channels\n",
    "    obj = cp.Maximize(cp.sum(cp.log(alpha + x)))\n",
    "\n",
    "    # Declare constraints\n",
    "    constraints = [x >= 0, cp.sum(x) - sum_x == 0]\n",
    "      \n",
    "    # Solve\n",
    "    prob = cp.Problem(obj, constraints)\n",
    "    prob.solve()\n",
    "    if(prob.status=='optimal'):\n",
    "        return prob.status, prob.value, x.value\n",
    "    else:\n",
    "        return prob.status, np.nan, np.nan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "As a simple example, we set $N = 3$, $P = 1$ and $\\boldsymbol{\\alpha} = (0.8,1.0,1.2)$.\n",
    "\n",
    "The function outputs whether the problem status, the maximum communication rate and the power allocation required is achieved with this maximum communication rate."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# As an example, we will solve the water filling problem with 3 buckets, each with different α\n",
    "np.set_printoptions(precision=3) \n",
    "buckets = 3\n",
    "alpha = np.array([0.8, 1.0, 1.2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Problem status: optimal\n",
      "Optimal communication rate = 0.863 \n",
      "Transmitter powers:\n",
      "[0.533 0.333 0.133]\n"
     ]
    }
   ],
   "source": [
    "stat, prob, x = water_filling(buckets, alpha)\n",
    "print('Problem status: {}'.format(stat))\n",
    "print('Optimal communication rate = {:.4g} '.format(prob))\n",
    "print('Transmitter powers:\\n{}'.format(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "To illustrate the water filling principle, we will plot $\\alpha_i + x_i$ and check that this level is flat where power has been allocated:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "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bWvlommEXAclq3VjahdatmJVlyuQo4PCL+0F7diFgONAMVWzBmZlYbNUswEfEW6Qb96LJNo0mjySqS9AXgamBsRNzQ0XmUHmv3H6TBA2ZmVie1nmh5LnCVpAeBGcDRwObAFABJPweIiMOz9weTWi4nAdMktbR+3oqIxVmd04D7gTnABsCxpARTcWSamZnVRk0TTERcK+n9wHjSfJUngH1K7qmUz4c5mhTjj7NXi3uAUdn3GwGXkLreXgEeAXaLiAeLuAYzM8un5kvFRMSFwIVtbBvV3vs29vkm8M1qxGZmZtXjxS7NzKwQTjBmZlYIJxgzMyuEE4yZmRXCCcbMzArhBGNm1mDGjh3L6aefXu8wOuQEY2ZWJ7NmzWLPPfekb9++DBkyhGnTpnHdddcxfPjwqp3j+uuvZ6211uLZZ1cs4chxxx3HlltuyYIF7a7S1W1+ZLKZ9Synb1in877SqeoPPfQQI0eO5NRTT2XKlClMmDCB0047jcWLF3PeeedVLazPfe5znHXWWfzgBz/g0ksv5ZxzzuGaa65hxowZbLrpplU7TyVOMGZmdXDiiSey3377MX78eAAOPfRQ9ttvP3bbbTf22GOPqp1HEpMmTWLMmDFsueWWTJw4kbvuuouttip+PWAnGDPrWTrZkqiH+fPnM336dKZOnbqirE+fPrz77rucccYZrepPmjSJSZMmrXj/5ptvIolzzjlnRdktt9zCrruWP00+2WuvvRg2bBjjx4/nxhtvZNiwYVW8mrY5wZiZ1dhTTz0FsNIH/ezZs9l6660ZMWJEq/pHH300X/jCF1a8P+WUUxg4cCDHHnvsirKBAwe2eb677rqLRx99lIgovFuslBOMmVmNvfzyy0iiV680zuq1115j4sSJbLZZ5Yf79uvXj379+q14v/7669OvXz+GDBnS4bkeffRRDjjgAC644AJuuukmxo0bx6233lqdC+mAR5GZmdXY0KFDiQgmT57M7NmzOeywwxgwYABz585lzpw5VTvPs88+yz777MMJJ5zAkUceyYQJE7j99tu5++67q3aO9jjBmJnV2BZbbMHEiRO56KKL2GGHHVh//fW544472H777dlll12qco7Fixez9957s++++/K9730PgO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iexTUFuADYKWvxIelK4HNl+ywHvpSt5vyEpFOA/5E0Ltt+ArBXy3GBp7Ofz9dIKwu3uCAibuhm/NZgnGCsp5hG+kscoB/wVeA2SR+PiPZaCaV2BB4rSS6VDATuBL4fET8s27YTMETSQSVlyr5uCbyYM45yJwP3Szqni/u3eKzk+wXZ18fLyjYp36fsUQEzgT6k61mL9GjgP5WtBN2b1LVXqrmLMVsDc4KxnmJJRLQ8SRBJs0hLph9FuofxbsumkjrlN5tFxxaRPjwPlnRZRLxUsq0XcBnp/k+5eTmOXVFEPCTp16SuqTPKNre6LtIHfCVvlx42O3Z5WWe6zVvq7geUP2vo7bL3b3TiuNZDOMFYTxWkD991svcLs68DSuoMLdvnYeCLkvq304p5E/gMcCNwu6TRJUnmYWC70kRXwVvAGjmvodSpwJPA3mXlpdfV8n35dXXHRyStGxEtCeITpGuYS0owbwIfjIi7qnhO6yF8k996irUkbZa9tiXdX1iPlAjIbvbfD5wiaTtJuwDlXU6/JHVj/U7SrpK2kPSZ8lFk2bH2I7WQbpe0UbbpLOBjkqZI2lHSEEn7Srq4ZPdnsjqDJfWXlOv/YJa0LiENFCj1N9L9ptMlfVjSXsD48v27YU3gZ9nPbDRwJnBpRLwREa+RfobnSDoyu96hko6WdFS7R7XVghOM9RSfBF7IXg8Aw4DPR8TdJXWOzL4+BFxM2Qdx9lf6SFJ31o3AX0gjr1o9lS9LMvtSkmQi4jFgN9IotHuAR0mjpxaU7HoOqQXwJKnFMagT1/h94J2yON4m3fz/UHa+CaTWTrXcQ/o5TCXNK7qLdE+oxXdJI89OyurdThpt9nQVY7AG5SdamplZIdyCMTOzQjjBmJlZIZxgzMysEE4wZmZWCCcYMzMrhBOMmZkVwgnGzMwK4QRjZmaF+D9QVlm86O56KAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib\n",
    "import matplotlib.pylab as plt\n",
    "%matplotlib inline\n",
    "\n",
    "matplotlib.rcParams.update({'font.size': 14})\n",
    "\n",
    "axis = np.arange(0.5,buckets+1.5,1)\n",
    "index = axis+0.5\n",
    "X = x.copy()\n",
    "Y = alpha + X\n",
    "\n",
    "# to include the last data point as a step, we need to repeat it\n",
    "A = np.concatenate((alpha,[alpha[-1]]))\n",
    "X = np.concatenate((X,[X[-1]]))\n",
    "Y = np.concatenate((Y,[Y[-1]]))\n",
    "\n",
    "plt.xticks(index)\n",
    "plt.xlim(0.5,buckets+0.5)\n",
    "plt.ylim(0,1.5)\n",
    "plt.step(axis,A,where='post',label =r'$\\alpha$',lw=2)\n",
    "plt.step(axis,Y,where='post',label=r'$\\alpha + x$',lw=2)\n",
    "plt.legend(loc='lower right')\n",
    "plt.xlabel('Bucket Number')\n",
    "plt.ylabel('Power Level')\n",
    "plt.title('Water Filling Solution')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
